package com.ycsj.goldmedalnewconcept.teacher.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.ycsj.common.base.CommonAdapter;
import com.ycsj.common.base.ViewHolder;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.teacher.bean.CommitAnswerInfo;

import java.util.List;


/**
 * 展示题目的面板的GridView的适配器
 * Created by Administrator on 2018/9/19 0019.
 */

public class PanelGvAdapter extends CommonAdapter<CommitAnswerInfo> {
    private Context context;
    private onTvClickListener listener;

    //用于区分:学生未作答,需要根据学生作答情况来进行状态的设置
    //老师查看学生有人作答和没人作答,不需要进行状态的设置
    private String type;

    public PanelGvAdapter(Context context, String type, List<CommitAnswerInfo> datas, int layoutId) {
        super(context, datas, layoutId);
        this.context = context;
        this.type = type;
    }

    @Override
    public void convert(ViewHolder holder, CommitAnswerInfo info) {
        final int position = holder.getPosition();
        TextView tv = holder.getView(R.id.tv);
        tv.setText(info.name);

        if ("0".equals(type)) {
            //根据完成的状态来设置背景和文字颜色
            //根据每个小题的onwAnswer来判断该题是否作答,不为空时表示已作答
            if (!TextUtils.isEmpty(info.onwAnswer)) {
                //已完成
                tv.setBackgroundResource(R.drawable.tk_tv_competed);
                tv.setTextColor(context.getResources().getColor(R.color.white));
            } else {
                tv.setBackgroundResource(R.drawable.tk_tv_no_competed);
                tv.setTextColor(context.getResources().getColor(R.color.nocomplete));
            }
        } else if ("1".equals(type)) {
            //学生查看答题详情
            if ("9".equals(info.typeid)) {
                //主观题
                tv.setBackgroundResource(R.drawable.tk_tv_zgt);
                tv.setTextColor(context.getResources().getColor(R.color.white));
            } else {
                //非主观题
                if (info.isRight) {
                    //作答正确
                    tv.setBackgroundResource(R.drawable.tk_tv_right);
                    tv.setTextColor(context.getResources().getColor(R.color.white));
                } else {
                    //作答错误
                    tv.setBackgroundResource(R.drawable.tk_tv_error);
                    tv.setTextColor(context.getResources().getColor(R.color.white));
                }
            }

        }


        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onClick(position);
                }
            }
        });
    }


    public void setOnTvClickListener(onTvClickListener listener) {
        this.listener = listener;
    }

    /**
     * 回调事件
     */
    public interface onTvClickListener {
        void onClick(int pos);
    }
}
